import logging

from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect_to

from sc_console.lib.base import BaseController, render

from sqlalchemy.sql import select
from sc_console.model.models import Heartbeat, HeartbeatResponse
from sc_console.model.meta import Session

log = logging.getLogger(__name__)

class StatusController(BaseController):
    app_query = select([HeartbeatResponse.app_name]).distinct()

    def index(self):
        c.heartbeats = Session.query(Heartbeat)\
            .order_by(Heartbeat.beat_time.desc())\
            .limit(8).all()

        apps = [x[0] for x in Session.connection().execute(self.app_query)]
        c.apps = apps

        return render('/status/index.mako')
